package com.foo.web.controller.wechat;

import java.util.List;

import com.foo.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxConfig;
import com.foo.wechat.service.IWxConfigService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;
import com.foo.common.utils.poi.ExcelUtil;
import com.foo.common.core.page.TableDataInfo;

/**
 * 项目配置Controller
 * 
 * @author foo
 * @date 2020-04-30
 */
@Controller
@RequestMapping("/wechat/config")
public class WxConfigController extends BaseController
{
    private String prefix = "wechat/config";

    @Autowired
    private IWxConfigService wxConfigService;

    @RequiresPermissions("wechat:config:view")
    @GetMapping()
    public String config(ModelMap mmap)
    {
        List<WxConfig> list = wxConfigService.selectWxConfigList(null);
        mmap.put("list",list);
        return prefix + "/config";
    }

    /**
     * 查询项目配置列表
     */
    @RequiresPermissions("wechat:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxConfig wxConfig)
    {
        startPage();
        List<WxConfig> list = wxConfigService.selectWxConfigList(wxConfig);
        return getDataTable(list);
    }

    /**
     * 导出项目配置列表
     */
    @RequiresPermissions("wechat:config:export")
    @Log(title = "项目配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxConfig wxConfig)
    {
        List<WxConfig> list = wxConfigService.selectWxConfigList(wxConfig);
        ExcelUtil<WxConfig> util = new ExcelUtil<WxConfig>(WxConfig.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 新增项目配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目配置
     */
    @RequiresPermissions("wechat:config:add")
    @Log(title = "项目配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxConfig wxConfig)
    {
        wxConfig.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(wxConfigService.insertWxConfig(wxConfig));
    }

    /**
     * 修改项目配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WxConfig wxConfig = wxConfigService.selectWxConfigById(id);
        mmap.put("wxConfig", wxConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目配置
     */
    @RequiresPermissions("wechat:config:edit")
    @Log(title = "项目配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxConfig wxConfig)
    {
        wxConfig.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(wxConfigService.updateWxConfig(wxConfig));
    }

    /**
     * 删除项目配置
     */
    @RequiresPermissions("wechat:config:remove")
    @Log(title = "项目配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wxConfigService.deleteWxConfigByIds(ids));
    }
}
